<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">


<meta name="google-site-verification" content="E9deYnivN5MuHMuIfiMZZfS0alv-d_0UjcwjBL79lGU" />



<meta name="baidu-site-verification" content="iHYWJxscwD" />










<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />



  <meta name="google-site-verification" content="true" />








  <meta name="baidu-site-verification" content="true" />







  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="网络课程,学习笔记,量化投资,机器学习,线性回归," />










<meta name="description" content="网络课程，B站上有视频。我是先看的吴恩达的视频，有好多个版本，尤其是早期还用matlab写代码，看不下去来看这个。这个蛮好，讲得很细，代码都用python和R实操。打算先把这个学完。第0部分1.机器学习应用略2.机器学习是未来数据很丰富。数据量暴增，只有用机器学习来处理。3、4.安装开发环境略。5.下载数据集https:&#x2F;&#x2F;www.superdatascience.com&#x2F;pages&#x2F;下载数据集第">
<meta property="og:type" content="article">
<meta property="og:title" content="量化投资学习笔记89——机器学习A-Z课程笔记01:数据预处理及回归">
<meta property="og:url" content="https://zwdnet.github.io/2020/09/18/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B089%E2%80%94%E2%80%94%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0A-Z%E8%AF%BE%E7%A8%8B%E7%AC%94%E8%AE%B001-%E6%95%B0%E6%8D%AE%E9%A2%84%E5%A4%84%E7%90%86%E5%8F%8A%E5%9B%9E%E5%BD%92/index.html">
<meta property="og:site_name" content="赵瑜敏的口腔医学专业学习博客">
<meta property="og:description" content="网络课程，B站上有视频。我是先看的吴恩达的视频，有好多个版本，尤其是早期还用matlab写代码，看不下去来看这个。这个蛮好，讲得很细，代码都用python和R实操。打算先把这个学完。第0部分1.机器学习应用略2.机器学习是未来数据很丰富。数据量暴增，只有用机器学习来处理。3、4.安装开发环境略。5.下载数据集https:&#x2F;&#x2F;www.superdatascience.com&#x2F;pages&#x2F;下载数据集第">
<meta property="og:locale">
<meta property="og:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/62/01.png">
<meta property="og:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/62/02.png">
<meta property="og:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/62/03.png">
<meta property="og:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/62/04.png">
<meta property="og:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/62/05.png">
<meta property="og:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/62/06.png">
<meta property="og:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/62/07.png">
<meta property="og:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/wx.jpg">
<meta property="article:published_time" content="2020-09-18T01:21:34.000Z">
<meta property="article:modified_time" content="2020-09-27T06:13:25.000Z">
<meta property="article:author" content="赵瑜敏">
<meta property="article:tag" content="网络课程">
<meta property="article:tag" content="学习笔记">
<meta property="article:tag" content="量化投资">
<meta property="article:tag" content="机器学习">
<meta property="article:tag" content="线性回归">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/62/01.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://zwdnet.github.io/2020/09/18/量化投资学习笔记89——机器学习A-Z课程笔记01-数据预处理及回归/"/>





  <title>量化投资学习笔记89——机器学习A-Z课程笔记01:数据预处理及回归 | 赵瑜敏的口腔医学专业学习博客</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">赵瑜敏的口腔医学专业学习博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://zwdnet.github.io/2020/09/18/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B089%E2%80%94%E2%80%94%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0A-Z%E8%AF%BE%E7%A8%8B%E7%AC%94%E8%AE%B001-%E6%95%B0%E6%8D%AE%E9%A2%84%E5%A4%84%E7%90%86%E5%8F%8A%E5%9B%9E%E5%BD%92/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/tx.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="赵瑜敏的口腔医学专业学习博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">量化投资学习笔记89——机器学习A-Z课程笔记01:数据预处理及回归</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-09-18T01:21:34+00:00">
                2020-09-18
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84/" itemprop="url" rel="index">
                    <span itemprop="name">量化投资</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  7
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>网络课程，B站上有视频。我是先看的吴恩达的视频，有好多个版本，尤其是早期还用matlab写代码，看不下去来看这个。这个蛮好，讲得很细，代码都用python和R实操。打算先把这个学完。<br>第0部分<br>1.机器学习应用<br>略<br>2.机器学习是未来<br>数据很丰富。数据量暴增，只有用机器学习来处理。<br>3、4.安装开发环境<br>略。<br>5.下载数据集<br><a target="_blank" rel="noopener" href="https://www.superdatascience.com/pages/%E4%B8%8B%E8%BD%BD%E6%95%B0%E6%8D%AE%E9%9B%86">https://www.superdatascience.com/pages/下载数据集</a><br>第1部分 数据预处理<br>6.数据预处理<br>导入数据</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pd.read_csv()</span><br></pre></td></tr></table></figure>
<p>7.处理缺失数据</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> sklearn.preprocessing <span class="keyword">import</span> Imputer</span><br><span class="line">    x = dataset.iloc[:, :-<span class="number">1</span>].values</span><br><span class="line">    y = dataset.iloc[:, <span class="number">3</span>].values</span><br><span class="line">    print(x, y)</span><br><span class="line">    imputer = Imputer(missing_values = <span class="string">&quot;NaN&quot;</span>, strategy = <span class="string">&quot;mean&quot;</span>, axis = <span class="number">0</span>)</span><br><span class="line">    imputer = imputer.fit(x[:, <span class="number">1</span>:<span class="number">3</span>])</span><br><span class="line">    x[:, <span class="number">1</span>:<span class="number">3</span>] = imputer.transform(x[:, <span class="number">1</span>:<span class="number">3</span>])</span><br><span class="line">    print(x)</span><br></pre></td></tr></table></figure>
<p>8.分类数据的处理</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 将分类数据编码</span></span><br><span class="line">labelencoder_x = LabelEncoder()</span><br><span class="line">x[:, <span class="number">0</span>] = labelencoder_x.fit_transform(x[:, <span class="number">0</span>])</span><br><span class="line">print(x)</span><br></pre></td></tr></table></figure>
<p>如此处理会使分类数据有大小关系，用虚拟编码解决。<br>将所有可能分类都作为一列，然后用0/1表示是否属于该类。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 虚拟编码</span></span><br><span class="line">onehotEncoder = OneHotEncoder(categorical_features = [<span class="number">0</span>])</span><br><span class="line">x = onehotEncoder.fit_transform(x).toarray()</span><br><span class="line"><span class="comment"># 处理因变量，不是必要的</span></span><br><span class="line">labelencoder_y = LabelEncoder()</span><br><span class="line">y = labelencoder_y.fit_transform(y)</span><br><span class="line">print(y)</span><br></pre></td></tr></table></figure>
<p>9.划分训练集和测试集<br>算法从训练集学习，用测试集来测试。<br>划分的原因:学习的是模型不是数据本身。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 划分训练集和测试集</span></span><br><span class="line">x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = <span class="number">0.2</span>, random_state = <span class="number">0</span>)</span><br><span class="line">print(x_train, y_train, x_test, y_test)</span><br></pre></td></tr></table></figure>
<p>10.特征缩放<br>处理不同的变量不在同一数量级上的情况，如年龄和收入。<br>方法有标准化(standardisation)和正态化(Normalisation)<br>标准化: x = [x-mean(x)]/std(x)<br>正态化: x = [x-min(x)]/[max(x)-min(x)]</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 特征缩放</span></span><br><span class="line">sc_x = StandardScaler()</span><br><span class="line">x_train = sc_x.fit_transform(x_train)</span><br><span class="line">x_test = sc_x.transform(x_test)</span><br><span class="line">print(x_train, x_test)</span><br></pre></td></tr></table></figure>
<p>因变量是分类变量，就不必了。如果是定量变量，是可能需要的。<br>11.数据预处理模板<br>特征工程还是很重要的，也许不能靠模板，pass吧。<br>总结一下数据处理数据的过程:读取数据(pd.read_csv)，处理缺失数据(sklearn.preprocessing.Imputer)，对分类数据进行编码(sklearn.preprocessing.LabelEncoder, OneHotEncoder)，特征缩放(正则化，sklearn.preprocessing.StandardScaler)，划分训练集和测试集(sklearn.model_selection.train_test_split)。<br>第2部分 简单线性回归<br>一元线性回归。<br>1.第一步<br>模型:线性方程<br>y = b0 + b1x1<br>y是因变量，x1是自变量。<br>2.第二步<br>拟合线性模型<br>对数据集中每个点，做垂直于x轴的直线与预测直线相交，交点横坐标xi，纵坐标为预测值yi’，找到一个参数组合b0，b1，使得所有点的(yi - yi’)²之和最小。<br>3.实操<br>进行数据预处理<br>没有缺失数据，也没有分类变量，使用sklearn线性回归也自带了特征缩放，所以直接划分训练集和测试集。<br>4.实操，进行线性回归</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 进行线性回归</span></span><br><span class="line">regress = LinearRegression()</span><br><span class="line">regress.fit(x_train, y_train)</span><br></pre></td></tr></table></figure>
<p>5.实操，用模型进行预测</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 用模型进行预测，用测试集来预测</span></span><br><span class="line">y_pred = regress.predict(x_test)</span><br><span class="line">print(y_pred, y_test)</span><br></pre></td></tr></table></figure>
<p>6.实操，对结果画图比较<br>先画训练集</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 结果绘图</span></span><br><span class="line">plt.figure()</span><br><span class="line">plt.scatter(x_train, y_train, color = <span class="string">&quot;red&quot;</span>)</span><br><span class="line">plt.plot(x_train, regress.predict(x_train), color = <span class="string">&quot;blue&quot;</span>)</span><br><span class="line">plt.title(<span class="string">&quot;Salary vs Expernce (trainning set)&quot;</span>)</span><br><span class="line">plt.xlabel(<span class="string">&quot;Year of Expernce&quot;</span>)</span><br><span class="line">plt.ylabel(<span class="string">&quot;Salary&quot;</span>)</span><br><span class="line">plt.savefig(<span class="string">&quot;P2_train.png&quot;</span>)</span><br></pre></td></tr></table></figure>
<p><img src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/62/01.png"><br>再画测试集</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 测试集结果</span></span><br><span class="line">plt.figure()</span><br><span class="line">plt.scatter(x_test, y_test, color = <span class="string">&quot;red&quot;</span>)</span><br><span class="line">plt.plot(x_train, regress.predict(x_train), color = <span class="string">&quot;blue&quot;</span>)</span><br><span class="line">plt.title(<span class="string">&quot;Salary vs Expernce (trainning set)&quot;</span>)</span><br><span class="line">plt.xlabel(<span class="string">&quot;Year of Expernce&quot;</span>)</span><br><span class="line">plt.ylabel(<span class="string">&quot;Salary&quot;</span>)</span><br><span class="line">plt.savefig(<span class="string">&quot;P2_test.png&quot;</span>)</span><br></pre></td></tr></table></figure>
<p><img src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/62/02.png"><br>结果不错。<br>多元线性回归。<br>1.模型<br>y = b0+b1x1+b2x2+……+bnxn<br>有多个自变量。<br>几个假设:<br>①线性<br>②同方差性<br>③多元正态分布<br>④误差独立<br>⑤无多重共线性<br>2.虚拟变量(dummy variables)<br>分类数据无法排序。如果是二分类变量，用其中一个状态的虚拟变量就可表示(非此即彼)。<br>虚拟变量陷阱:如果把二分类变量的两个变量都纳入模型，二者有确定性的关系，会产生多重共线性，不满足多元线性回归的假设。参数过多会有过拟合的问题。多分类变量也是如此，永远要省略掉一个虚拟变量。<br>3.建模步骤<br>要舍弃一些自变量。原因:垃圾进，垃圾出。<br>五种方法:<br>①全部变量纳入<br>②反向淘汰<br>③顺向选择<br>④双向淘汰<br>⑤信息量比较<br>②③④称为逐步回归。<br>①全部纳入<br>a.基于先验知识<br>b.必须为之<br>c.为反向淘汰做准备<br>②反向淘汰<br>a.选择一个显著性水平(SL)<br>b.用所有变量建模<br>c.计算各变量的P值，若最大的p值大于SL，进入第四步。否则(所有变量P值小于等于SL，结束)。<br>d.从模型中去除P值最高的变量。<br>e.用剩下的自变量重复c,d步。<br>③顺向淘汰<br>a.选择一个显著性水平(SL)<br>b.对所有自变量分别进行简单一元线性回归，选择P值最低的变量。<br>c.保留该变量，依次添加额外的一个变量进行建模。<br>d.如果有P值小于SL，重复第三步，否则结束。<br>④双向淘汰<br>a.选择一个进入模型和存留在模型中的显著性水平(SLENTER和SLSTAY)<br>b.按照顺向淘汰的方法选择一个变量进入模型(P&lt;SLENTER)<br>c.按照反向淘汰的方法剔除变量(P&gt;SLSTATY)<br>d.当没有变量能进入和退出时结束。<br>⑤信息量比较<br>a.选择一个方法对模型打分。(例如Akaike criterion)<br>b.建立所有的可能的模型(对n个自变量有2^n-1个模型)<br>c.选择评分最高的模型。<br>4.实操<br>读取数据，有一列分类数据。用OneHot编码转换。<br>然后进行建模和预测，跟一元线性回归一样的。<br>上面是采用全部纳入的策略，下面再用其它策略试试。<br>用statsmodels库来输出回归的具体结果。<br>多元线性回归，要加上常数项b0x0，其中x0=1。<br>我在手机上没法装statsmodels库，只有到云服务器里弄了。<br>程序运行老有问题，先跳过吧。<br>多项式线性回归<br>1.原理<br>y = b0+b1x1+b2x1^2+…+bnx1^n<br>问题，为什么称为”线性”?<br>指的是方程参数是不是线性组合。<br>2.实操<br>读取数据，画散点图看看。<br><img src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/62/03.png"><br>明显是非线性的，用多项式回归。<br>因为数据量小，就不划分训练集和测试集。<br><img src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/62/04.png"><br>蓝线是线性回归，绿线是二次多项式回归。可以看到好了很多，但还可以改进。升高多项式次数。<br><img src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/62/05.png"><br>升高到三次多项式，黑色的，拟合的更好了。<br>再升高到四次看看。<br><img src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/62/06.png"><br>更准了，但是也过拟合了。<br>平滑曲线<br><img src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/62/07.png"><br>3.评价模型<br>R平方值<br>预测值和真实值的差的平方之和称为剩余平方和(SSres)，回归即使该值最小。<br>预测值与真实值平均值之差的平方之和为共平方和(SStot)。<br>R² = 1 - SSres/SStot<br>范围0-1，值越大，拟合越精准。<br>调整R平方值(Adjusted R²)<br>新增加一个自变量后，R平方值不会减小。即增加自变量几乎一定会改善拟合度，但模型未必会更好。因此定义调整R平方值。<br>AR² = 1-(1-R²)(n-1)/(n-p-1)<br>p:自变量个数<br>n:数据个数<br>代码：<a target="_blank" rel="noopener" href="https://github.com/zwdnet/MyQuant/tree/master/49">https://github.com/zwdnet/MyQuant/tree/master/49</a></p>
<p>我发文章的三个地方，欢迎大家在朋友圈等地方分享，欢迎点“在看”。<br>我的个人博客地址：<a href="https://zwdnet.github.io/">https://zwdnet.github.io</a><br>我的知乎文章地址： <a target="_blank" rel="noopener" href="https://www.zhihu.com/people/zhao-you-min/posts">https://www.zhihu.com/people/zhao-you-min/posts</a><br>我的微信个人订阅号：赵瑜敏的口腔医学学习园地</p>
<p><img src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/wx.jpg"></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>欢迎打赏！感谢支持！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/mm_facetoface_collect_qrcode_1542944836634.png" alt=" 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/1542944857770.jpg" alt=" 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/%E7%BD%91%E7%BB%9C%E8%AF%BE%E7%A8%8B/" rel="tag"># 网络课程</a>
          
            <a href="/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" rel="tag"># 学习笔记</a>
          
            <a href="/tags/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84/" rel="tag"># 量化投资</a>
          
            <a href="/tags/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/" rel="tag"># 机器学习</a>
          
            <a href="/tags/%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92/" rel="tag"># 线性回归</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/09/12/RBFDPS%E7%9A%84%E5%8F%A3%E5%86%85%E8%AF%95%E6%88%B4%E3%80%81%E7%B2%98%E6%8E%A5%E5%8F%8A%E9%97%AE%E9%A2%98%E5%A4%84%E7%90%86%E2%80%94%E2%80%942019%E9%94%90%E8%AF%BB%E4%BC%9A%E5%A4%A7%E7%BB%93%E5%B1%80/" rel="next" title="RBFDPS的口内试戴、粘接及问题处理——2019锐读会大结局">
                <i class="fa fa-chevron-left"></i> RBFDPS的口内试戴、粘接及问题处理——2019锐读会大结局
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/09/18/%E5%B9%B4%E8%BD%BB%E6%81%92%E7%89%99%E5%A4%96%E4%BC%A4%E6%8A%98%E6%96%AD%E7%9A%84%E5%A4%84%E7%90%86/" rel="prev" title="年轻恒牙外伤折断的处理">
                年轻恒牙外伤折断的处理 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC80MTA2Mi8xNzU4Nw=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/tx.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%20%7C%7C%20archive">
              
                  <span class="site-state-item-count">452</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">29</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">544</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">本站版权归赵瑜敏所有，如欲转载请与本人联系。</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">1225.8k</span>
  
</div>









<div>
  <script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1275447216'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s11.cnzz.com/z_stat.php%3Fid%3D1275447216%26online%3D1%26show%3Dline' type='text/javascript'%3E%3C/script%3E"));</script>
</div>

        







  <div style="display: none;">
    <script src="//s95.cnzz.com/z_stat.php?id=1275447216&web_id=1275447216" language="JavaScript"></script>
  </div>



        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  

  

  
  

  

  

  

  
</body>
</html>
